package com.cxy.controller.user;

import com.cxy.dto.UpdatePassWordDTO;
import com.cxy.dto.UserLoginDTO;
import com.cxy.entity.User;
import com.cxy.result.Result;
import com.cxy.service.UserService;
import com.cxy.utils.JwtUtils;
import com.cxy.vo.AdminLoginVO;
import com.cxy.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
@Api(tags = "用户端登录接口")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserLoginController {
    @Autowired
    private UserService userService;
    /**
     * 用户登录
     */
    @ApiOperation("用户端登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        //通过密码和账号查看用户是否存在
        User user = userService.login(userLoginDTO);
        //登录成功下发jwt令牌
        if (user!=null){
            Map<String,Object> map =new HashMap<>();
            map.put("id",user.getId());
            String jwt = JwtUtils.generateJwt(map);
            UserLoginVO userLoginVO =new UserLoginVO();
            userLoginVO.setId(user.getId());
            userLoginVO.setName(user.getName());
            userLoginVO.setUserName(user.getUsername());
            userLoginVO.setToken(jwt);
            return Result.success(userLoginVO);
        }
        return Result.error("密码或者用户名错误");
    }
    /**
     * 找回密码
     */
    //发送验证码
    @ApiOperation("用户端获取验证码功能")
    @GetMapping("/email/{email}")
    public Result sendEmailCode(@PathVariable String email){
        userService.sendEmailCode(email);
        return Result.success();
    }
    /**
     * 输入验证码并修改密码
     */
    @ApiOperation("用户端输入验证码并修改密码功能")
    @PutMapping("/reset")
   public Result reset(@RequestBody UpdatePassWordDTO updatePassWordDTO){
        userService.reset(updatePassWordDTO);
        return Result.success();
    }
}
